package com.zxz.mq;

import com.alibaba.fastjson.JSON;
import com.zxz.mq.dto.TransMessageDto;
import com.zxz.mq.service.IMessageSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MqSender {
    @Autowired
    @Qualifier("myRabbitAdmin")
    private RabbitAdmin myRabbitAdmin;

    @Autowired
    private IMessageSendService messageSendService;

    public void sendMessage(String exchange, String routingKey, String message){
        log.info("sendMessage():exchange:{}, routingKey:{}, message{}",exchange, routingKey, message);
        TransMessageDto transMessageDto = messageSendService.messageSendReady(exchange, routingKey, message);
        myRabbitAdmin.getRabbitTemplate().convertAndSend(exchange, routingKey, JSON.toJSONString(transMessageDto), new CorrelationData(transMessageDto.getId()));
        log.info("发送消息，消息ID:{}", transMessageDto.getId());
    }
}
